﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.MainModule.Entities;
using Application.MainModule.VirtualAccountsManagement;
using Infraestructure.CrossCutting.IoC;
using Infraestructure.CrossCutting.Logging;
using DistributedServices.Core.ErrorHandlers;
using System.ServiceModel;

namespace DistributedServices.MainModule
{
    public partial class MainModuleService
    {
        public List<VirtualAccount> GetVirtualAccountByUserId(Guid userId)
        {
            try
            {
                IVirtualAccountManagementService virtualAccountService = IoCFactory.Instance.CurrentContainer.Resolve<IVirtualAccountManagementService>();

                return virtualAccountService.FindVirtualAccounts(userId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public VirtualAccount GetVirtualAccountById(Guid virtualAccountId)
        {
            try
            {
                IVirtualAccountManagementService virtualAccountService = IoCFactory.Instance.CurrentContainer.Resolve<IVirtualAccountManagementService>();

                return virtualAccountService.FindVirtualAccountById(virtualAccountId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void ChangeVirtualAccount(VirtualAccount virtualAccount)
        {
            try
            {
                IVirtualAccountManagementService virtualAccountService = IoCFactory.Instance.CurrentContainer.Resolve<IVirtualAccountManagementService>();
                virtualAccountService.ChangeVirtualAccount(virtualAccount);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public void RemoveVirtualAccount(VirtualAccount virtualAccount)
        {
            try
            {
                IVirtualAccountManagementService virtualAccountService = IoCFactory.Instance.CurrentContainer.Resolve<IVirtualAccountManagementService>();
                virtualAccountService.RemoveVirtualAccount(virtualAccount);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void AddVirtualAccount(VirtualAccount virtualAccount)
        {
            try
            {
                IVirtualAccountManagementService virtualAccountService = IoCFactory.Instance.CurrentContainer.Resolve<IVirtualAccountManagementService>();
                virtualAccountService.AddVirtualAccount(virtualAccount);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

    }
}
